package cn.cytong.util;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: TangGaowei
 * Date: 12-9-28
 * Time: 下午10:15
 * To change this template use File | Settings | File Templates.
 */
public class StringTool {

    /**
     * 将字符串用零补齐到指定长度，超过指定长度的截取
     * @param src
     * @param length
     * @return
     */
    public static String fillNumber(String src, int length){
        StringBuffer result = new StringBuffer("");

        if(isEmpty(src)){
            for(int i=0;i<length;i++){
                result.append("0");
            }
        }
        else{
            src = src.trim();
            if(src.length() == length){
                result.append(src);
            }
            else if(src.length() > length){
                result.append(cutString(src, length));
            }
            else{
                int count = length - src.length();
                for(int i=0;i<count;i++){
                    result.append("0");
                }
                result.append(src);
            }
        }

        return result.toString();
    }

    /**
     * 替换字符为星号
     * @param source
     * @param firstLength
     * @param lastLength
     * @return
     */
    public static String replaceMiddle(String source, int firstLength, int lastLength){
        String result = source;
        if(source != null){
            int length = result.length();
            if(length > firstLength + lastLength){
                result = source.substring(0, firstLength);
                StringBuffer temp = new StringBuffer("");
                for(int i=0;i<length - firstLength - lastLength;i++){
                    temp.append("*");
                }
                result += temp.toString();
                result += lastString(source, lastLength);
            }
        }

        return result;
    }




    /**
     * 从前面截取字符串
     * @param str
     * @param length
     * @return
     */
    public static String firstString(String str, Integer length){
        String result = "";

        if(StringTool.isNotEmpty(str)){
            if(str.length() > length){
                result = str.substring(0, length);
            }
            else{
                result = str;
            }
        }
        return result;
    }

    /**
     * 从后面截取字符串
     * @param str
     * @param length
     * @return
     */
    public static String lastString(String str, Integer length){
        String result = "";

        if(StringTool.isNotEmpty(str)){
            int len = str.length();
            if(len > length){
                result = str.substring((len -length), len);
            }
            else{
                result = str;
            }
        }
        return result;
    }

    /**
     * 打印字符串列表
     * @param list
     */
    public static void print(List<String> list){
        if(list != null){
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i));
            }
        }
    }

    /**
     * 是否为数据
     * @param str
     * @return
     */
    public static boolean isNumeric(String str){
        if(isEmpty(str)){
            return false;
        }
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }

    /**
     * str 是否在 arr 内
     * @param arr
     * @param str
     * @return
     */
    public static boolean exists(String[] arr, String str){
        for (int i = 0; i < arr.length; i++) {
            String s = arr[i];
            if(s.equals(str)){
                return true;
            }
        }

        return false;
    }

    /**
     * 截断字符串，加上“...”
     * @param str
     * @param length
     * @return
     */
    public static String cutString (String str, Integer length){
        String result = str==null?null:str.replaceAll("<.*?>","").replaceAll("&.*?;","");
        if(result != null && result.length() > length){
            result = result.substring(0, length) + "...";
        }
        return result;
    }

    public static boolean isNull(String str){
        if(str == null){
            return true;
        }else{
            return false;
        }
    }

    public static boolean isNotEmpty(String str){
        return !isEmpty(str);
    }

    public static boolean isEmpty(String str){
        if(str == null || str.trim().length() == 0){
            return true;
        }else{
            return false;
        }
    }

    public static String toString(long value){
        return (new Long(value)).toString();
    }

    public static boolean contained(String source, String part){
        boolean ret = false;

        if(!isEmpty(source)){
            if(source.indexOf(part) >= 0){
                ret = true;
            }
        }
        return ret;
    }

    public static String toUpperFirstLetter(String source){
        String str = "";

        String firstLetter = source.substring(0, 1);

        str = firstLetter.toUpperCase() + source.substring(1);

        return str;
    }

    public static String toString(String[] arr){
        return toString(arr, ",");
    }

    public static String toString(String[] arr, String part){
        String str = "";
        for (int i = 0; i < arr.length; i++) {
            if(i > 0){
                str += part;
            }

            str += arr[i];
        }

        return str;
    }

    public static String toString(Long[] arr, String part){
        String[] strings = new String[arr.length];
        for(int i=0; i<arr.length; i++){
            strings[i] = arr[i].toString();
        }
        return toString(strings, part);
    }

    public static String toString(Long[] arr){
        return toString(arr, ",");
    }

    public static String toString(Set set){
        return toString(set, ",");
    }

    public static String toString(Set set, String part){
        String str = "";

        int i = 0;
        for ( Iterator iterator = set.iterator(); iterator.hasNext(); ) {
            if(i>0){
                str += part;
            }

            str += iterator.next().toString();
            i++;
        }

        return str;
    }

    public static String toString(int n){
        return (new Integer(n)).toString();
    }

    /**
     * 定长输出 Integer 类型的字符串，长度不够的在前面补 0
     * @param number
     * @param length
     * @return
     */
    public static String toString(Integer number, int length){
        if(number == null){
            return "";
        }

        String result = number.toString();
        int numberLength = result.length();
        if(numberLength > length){
            result = result.substring(0, length);
        }
        else if(numberLength < length){
            for(int i=0; i<length-numberLength; i++){
                result = "0" + result;
            }
        }

        return result;
    }

    /**
     * 定长输出 Long 类型的字符串，长度不够的在前面补 0
     * @param number
     * @param length
     * @return
     */
    public static String toString(Long number, int length){
        if(number == null){
            return "";
        }

        String result = number.toString();
        int numberLength = result.length();
        if(numberLength > length){
            result = result.substring(0, length);
        }
        else if(numberLength < length){
            for(int i=0; i<length-numberLength; i++){
                result = "0" + result;
            }
        }

        return result;
    }



    /**
     * 定长输出 Hex 字符串类型的字符串，长度不够的在前面补 0
     * @param str
     * @param length
     * @return
     */
    public static String toHexString(String str, int length){
        if(str == null){
            return "";
        }

        String result = str;
        int numberLength = result.length();
        if(numberLength > length){
            result = result.substring(0, length);
        }
        else if(numberLength < length){
            for(int i=0; i<length-numberLength; i++){
                result = "0" + result;
            }
        }

        return result;
    }

    /**
     * 定长输出 String 类型的字符串，长度不够的在后面补空格
     * @param str
     * @param length
     * @return
     */
    public static String toString(String str, int length){
        if(str == null){
            return "";
        }

        String result = str;
        int numberLength = result.length();
        if(numberLength > length){
            result = result.substring(0, length);
        }
        else if(numberLength < length){
            for(int i=0; i<length-numberLength; i++){
                result += " ";
            }
        }

        return result;
    }

    public static String toRandomString(int length){
        long biggest = 10000000;

        String str = "1";
        for(int i=0; i<length; i++){
            str += "0";
        }

        biggest = Integer.valueOf(str);

        long b=(long)(Math.random()*biggest);//产生0-1000的整数随机数
        if(b >= biggest){
            b = biggest - 1;
        }
        return toString(b, length);
    }
}
